package com.xzj.common.core.utils;

import cn.hutool.core.util.StrUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: xzj-common
 * @ClassName: RegularUtil
 * @description: 正则基础类
 * @author: XiaZunJun
 * @create: 2024-06-17 10:02
 */
public class RegularUtil {

    /**
     * 正则匹配返回
     *
     * @return String
     * @author xiazunjun
     * @date 2023/4/18 16:53
     */
    public static String matchStr(String regular, String regularStr) {
        Pattern r = Pattern.compile(regular);
        Matcher m = r.matcher(regular);
        if (m.find()) {
            return m.group();
        }
        return null;
    }

    /**
     * 正则匹配替换
     *
     * @return String
     * @author xiazunjun
     * @date 2023/4/18 16:53
     */
    public static String regularReplace(String regular, String regularStr,String replaceStr) {
        if(StrUtil.isNotEmpty(regular) && StrUtil.isNotEmpty(regularStr) && null != replaceStr){
            return regularStr.replaceAll(regular, replaceStr);
        }
        return regularStr;
    }
}
